﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AyuBookmark.Common.Models;

namespace AyuBookmark.Common.Interfaces
{
    /// <summary>
    /// 后台服务代理接口，定义WinForm与后台服务的通信契约
    /// </summary>
    public interface IServiceProxy : IDisposable
    {
        #region 服务状态控制
        /// <summary>
        /// 检查服务是否正在运行
        /// </summary>
        /// <returns>运行状态</returns>
        Task<bool> IsServiceRunningAsync();

        /// <summary>
        /// 启动后台服务
        /// </summary>
        /// <returns>是否启动成功</returns>
        Task<bool> StartServiceAsync();

        /// <summary>
        /// 停止后台服务
        /// </summary>
        /// <returns>是否停止成功</returns>
        Task<bool> StopServiceAsync();

        /// <summary>
        /// 重启后台服务
        /// </summary>
        /// <returns>是否重启成功</returns>
        Task<bool> RestartServiceAsync();
        #endregion

        #region 书签操作
        /// <summary>
        /// 获取书签分页列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <param name="searchTerm">搜索关键词</param>
        /// <param name="categoryId">分类过滤</param>
        /// <returns>分页结果</returns>
        Task<PaginatedResult<Bookmark>> GetBookmarksAsync(
            int pageIndex = 1,
            int pageSize = 20,
            string searchTerm = null,
            int? categoryId = null);

        /// <summary>
        /// 根据ID获取书签详情
        /// </summary>
        /// <param name="id">书签ID</param>
        /// <returns>书签对象</returns>
        Task<Bookmark> GetBookmarkByIdAsync(int id);

        /// <summary>
        /// 添加或更新书签
        /// </summary>
        /// <param name="bookmark">书签数据</param>
        /// <returns>操作后的书签</returns>
        Task<Bookmark> SaveBookmarkAsync(Bookmark bookmark);

        /// <summary>
        /// 批量删除书签
        /// </summary>
        /// <param name="ids">书签ID集合</param>
        /// <returns>删除数量</returns>
        Task<int> DeleteBookmarksAsync(IEnumerable<int> ids);

        /// <summary>
        /// 导出书签数据
        /// </summary>
        /// <param name="filePath">导出路径</param>
        /// <param name="bookmarkIds">指定导出的书签ID</param>
        /// <returns>是否导出成功</returns>
        Task<bool> ExportBookmarksAsync(string filePath, IEnumerable<int> bookmarkIds = null);

        /// <summary>
        /// 导入书签数据
        /// </summary>
        /// <param name="filePath">导入文件路径</param>
        /// <returns>导入数量</returns>
        Task<int> ImportBookmarksAsync(string filePath);
        #endregion

        #region 分类管理
        /// <summary>
        /// 获取全部分类（树形结构）
        /// </summary>
        /// <returns>分类集合</returns>
        Task<IEnumerable<Category>> GetCategoriesAsync();

        /// <summary>
        /// 创建新分类
        /// </summary>
        /// <param name="category">分类数据</param>
        /// <returns>创建的分类</returns>
        Task<Category> CreateCategoryAsync(Category category);

        /// <summary>
        /// 更新分类信息
        /// </summary>
        /// <param name="category">分类数据</param>
        /// <returns>是否更新成功</returns>
        Task<bool> UpdateCategoryAsync(Category category);

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <param name="moveToId">转移书签的目标分类ID</param>
        /// <returns>是否删除成功</returns>
        Task<bool> DeleteCategoryAsync(int id, int? moveToId = null);
        #endregion

        #region 配置管理
        /// <summary>
        /// 获取配置值
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="key">配置键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>配置值</returns>
        Task<T> GetConfigAsync<T>(string key, T defaultValue = default);

        /// <summary>
        /// 设置配置值
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="key">配置键</param>
        /// <param name="value">配置值</param>
        /// <returns>是否设置成功</returns>
        Task<bool> SetConfigAsync<T>(string key, T value);

        /// <summary>
        /// 获取全部配置
        /// </summary>
        /// <returns>配置项集合</returns>
        Task<IEnumerable<ConfigItem>> GetAllConfigsAsync();
        #endregion

        #region 监控功能
        /// <summary>
        /// 获取当前监控状态
        /// </summary>
        /// <returns>监控状态对象</returns>
        Task<MonitoringStatus> GetMonitoringStatusAsync();

        /// <summary>
        /// 启用/禁用自动监控
        /// </summary>
        /// <param name="enable">是否启用</param>
        /// <returns>是否操作成功</returns>
        Task<bool> ToggleMonitoringAsync(bool enable);

        /// <summary>
        /// 设置停留时间阈值
        /// </summary>
        /// <param name="minutes">分钟数（0表示记录所有）</param>
        /// <returns>是否设置成功</returns>
        Task<bool> SetStayDurationAsync(int minutes);
        #endregion

        #region 事件通知
        /// <summary>
        /// 书签添加事件
        /// </summary>
        event EventHandler<BookmarkEventArgs> BookmarkAdded;

        /// <summary>
        /// 书签更新事件
        /// </summary>
        event EventHandler<BookmarkEventArgs> BookmarkUpdated;

        /// <summary>
        /// 书签删除事件
        /// </summary>
        event EventHandler<BookmarkEventArgs> BookmarkDeleted;

        /// <summary>
        /// 服务状态变更事件
        /// </summary>
        event EventHandler<ServiceStatusEventArgs> ServiceStatusChanged;
        #endregion
    }

    #region 事件参数类
    /// <summary>
    /// 书签事件参数
    /// </summary>
    public class BookmarkEventArgs : EventArgs
    {
        public Bookmark Bookmark { get; }
        public DateTime EventTime { get; }

        public BookmarkEventArgs(Bookmark bookmark)
        {
            Bookmark = bookmark;
            EventTime = DateTime.Now;
        }
    }

    /// <summary>
    /// 服务状态事件参数
    /// </summary>
    public class ServiceStatusEventArgs : EventArgs
    {
        public bool IsRunning { get; }
        public string Message { get; }

        public ServiceStatusEventArgs(bool isRunning, string message = null)
        {
            IsRunning = isRunning;
            Message = message;
        }
    }

    /// <summary>
    /// 监控状态信息
    /// </summary>
    public class MonitoringStatus
    {
        /// <summary>
        /// 是否正在监控
        /// </summary>
        public bool IsActive { get; set; }

        /// <summary>
        /// 当前停留时间阈值（分钟）
        /// </summary>
        public int StayDuration { get; set; }

        /// <summary>
        /// 最后活动时间
        /// </summary>
        public DateTime LastActivityTime { get; set; }

        /// <summary>
        /// 今日记录数量
        /// </summary>
        public int TodayCount { get; set; }
    }
    #endregion

    /// <summary>
    /// 服务代理工厂接口
    /// </summary>
    public interface IServiceProxyFactory
    {
        /// <summary>
        /// 创建服务代理实例
        /// </summary>
        /// <param name="connectionTimeout">连接超时(毫秒)</param>
        IServiceProxy CreateProxy(int connectionTimeout = 5000);
    }

    /// <summary>
    /// 服务代理选项配置
    /// </summary>
    public class ServiceProxyOptions
    {
        /// <summary>
        /// 管道名称（需与服务端一致）
        /// </summary>
        public string PipeName { get; set; } = "AyuBookmarkServicePipe";

        /// <summary>
        /// 连接超时时间（毫秒）
        /// </summary>
        public int ConnectionTimeout { get; set; } = 5000;

        /// <summary>
        /// 自动重试次数
        /// </summary>
        public int RetryCount { get; set; } = 3;

        /// <summary>
        /// 是否启用加密通信
        /// </summary>
        public bool EnableEncryption { get; set; } = true;
    }

}